<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android 10 Android音量调节 | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android 10 Android音量调节">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 10 Android音量调节">
<meta property="og:url" content="http://longzhiye.top/2024/01/06/2024-01-06/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android 10 Android音量调节">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-01-05T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.918Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Settings">
<meta property="article:tag" content="framework">
<meta property="article:tag" content="Launcher">
<meta name="twitter:card" content="summary">

  <meta name="keywords" content=",Android,Settings,framework,Launcher">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2024/01/06/2024-01-06/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2024/01/06/2024-01-06/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-4.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android 10 Android音量调节</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2024-01-06 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2024/01/06/2024-01-06/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android 10 Android音量调节&url=http://longzhiye.top/2024/01/06/2024-01-06/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android 10 Android音量调节&url=http://longzhiye.top/2024/01/06/2024-01-06/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2024/01/06/2024-01-06/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2024/01/06/2024-01-06/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2024/01/06/2024-01-06/&title=Android 10 Android音量调节" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android 10 Android音量调节&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2024/01/06/2024-01-06/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2024/01/06/2024-01-06/&text=Android 10 Android音量调节" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>在手机和平板上面，我们实际上能调节的也就5个音量:</p>
<table>
<thead>
<tr>
<th>流类型</th>
<th>最大音量</th>
<th>音量</th>
<th>默认音量</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>STREAM_VOICE_CALL</td>
<td>5</td>
<td>1</td>
<td>4</td>
<td>通话音量</td>
</tr>
<tr>
<td>STREAM_RING</td>
<td>7</td>
<td>0</td>
<td>5</td>
<td>铃声，通知音量等</td>
</tr>
<tr>
<td>STREAM_MUSIC</td>
<td>15</td>
<td>0</td>
<td>5</td>
<td>多媒体音量</td>
</tr>
<tr>
<td>STREAM_ALARM</td>
<td>7</td>
<td>0</td>
<td>6</td>
<td>闹钟音量</td>
</tr>
<tr>
<td>STREAM_BLUETOOTH_SCO</td>
<td>15</td>
<td>0</td>
<td>7</td>
<td>蓝牙音量</td>
</tr>
</tbody></table>
<h3 id="1-按键的处理流程"><a href="#1-按键的处理流程" class="headerlink" title="1 按键的处理流程"></a>1 按键的处理流程</h3><p><strong>frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// pre-condition: event.getKeyCode() is one of KeyEvent.KEYCODE_VOLUME_UP,</span></span><br><span class="line">    <span class="comment">//          KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">dispatchDirectAudioEvent</span><span class="params">(KeyEvent event)</span> &#123;</span><br><span class="line">        <span class="comment">// When System Audio Mode is off, volume keys received by AVR can be either consumed by AVR</span></span><br><span class="line">        <span class="comment">// or forwarded to the TV. It&#x27;s up to Amplifier manufacturer’s implementation.</span></span><br><span class="line">        <span class="type">HdmiControlManager</span> <span class="variable">hdmiControlManager</span> <span class="operator">=</span> getHdmiControlManager();</span><br><span class="line">        <span class="keyword">if</span> (<span class="literal">null</span> != hdmiControlManager</span><br><span class="line">                &amp;&amp; !hdmiControlManager.getSystemAudioMode()</span><br><span class="line">                &amp;&amp; shouldCecAudioDeviceForwardVolumeKeysSystemAudioModeOff()) &#123;</span><br><span class="line">            <span class="type">HdmiAudioSystemClient</span> <span class="variable">audioSystemClient</span> <span class="operator">=</span> hdmiControlManager.getAudioSystemClient();</span><br><span class="line">            <span class="keyword">if</span> (audioSystemClient != <span class="literal">null</span>) &#123;</span><br><span class="line">                audioSystemClient.sendKeyEvent(</span><br><span class="line">                        event.getKeyCode(), event.getAction() == KeyEvent.ACTION_DOWN);</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            getAudioService().handleVolumeKey(event, mUseTvRouting,</span><br><span class="line">                    mContext.getOpPackageName(), TAG);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            Log.e(TAG, <span class="string">&quot;Error dispatching volume key in handleVolumeKey for event:&quot;</span></span><br><span class="line">                    + event, e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">   <span class="comment">// 这里通过AIDL获取IAudioService的实例，</span></span><br><span class="line">    <span class="keyword">static</span> IAudioService <span class="title function_">getAudioService</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">IAudioService</span> <span class="variable">audioService</span> <span class="operator">=</span> IAudioService.Stub.asInterface(</span><br><span class="line">                ServiceManager.checkService(Context.AUDIO_SERVICE));</span><br><span class="line">        <span class="keyword">if</span> (audioService == <span class="literal">null</span>) &#123;</span><br><span class="line">            Log.w(TAG, <span class="string">&quot;Unable to find IAudioService interface.&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> audioService;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>在这里，通过Binder获取到了AudioService的实例，调用AudioService的handleVolumeKey()方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// pre-condition: event.getKeyCode() is one of KeyEvent.KEYCODE_VOLUME_UP,</span></span><br><span class="line">    <span class="comment">//                                   KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">handleVolumeKey</span><span class="params">(<span class="meta">@NonNull</span> KeyEvent event, <span class="type">boolean</span> isOnTv,</span></span><br><span class="line"><span class="params">            <span class="meta">@NonNull</span> String callingPackage, <span class="meta">@NonNull</span> String caller)</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">keyEventMode</span> <span class="operator">=</span> VOL_ADJUST_NORMAL;</span><br><span class="line">        <span class="keyword">if</span> (isOnTv) &#123;</span><br><span class="line">            <span class="keyword">if</span> (event.getAction() == KeyEvent.ACTION_DOWN) &#123;</span><br><span class="line">                keyEventMode = VOL_ADJUST_START;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123; <span class="comment">// may catch more than ACTION_UP, but will end vol adjustement</span></span><br><span class="line">                <span class="comment">// the vol key is either released (ACTION_UP), or multiple keys are pressed</span></span><br><span class="line">                <span class="comment">// (ACTION_MULTIPLE) and we don&#x27;t know what to do for volume control on CEC, end</span></span><br><span class="line">                <span class="comment">// the repeated volume adjustement</span></span><br><span class="line">                keyEventMode = VOL_ADJUST_END;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (event.getAction() != KeyEvent.ACTION_DOWN) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">int</span> <span class="variable">flags</span> <span class="operator">=</span> AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND</span><br><span class="line">                | AudioManager.FLAG_FROM_KEY;</span><br><span class="line">        <span class="keyword">switch</span> (event.getKeyCode()) &#123;</span><br><span class="line">            <span class="keyword">case</span> KeyEvent.KEYCODE_VOLUME_UP:</span><br><span class="line">                    adjustSuggestedStreamVolume(AudioManager.ADJUST_RAISE,</span><br><span class="line">                            AudioManager.USE_DEFAULT_STREAM_TYPE, flags, callingPackage, caller,</span><br><span class="line">                            Binder.getCallingUid(), <span class="literal">true</span>, keyEventMode);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> KeyEvent.KEYCODE_VOLUME_DOWN:</span><br><span class="line">                    adjustSuggestedStreamVolume(AudioManager.ADJUST_LOWER,</span><br><span class="line">                            AudioManager.USE_DEFAULT_STREAM_TYPE, flags, callingPackage, caller,</span><br><span class="line">                            Binder.getCallingUid(), <span class="literal">true</span>, keyEventMode);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> KeyEvent.KEYCODE_VOLUME_MUTE:</span><br><span class="line">                <span class="keyword">if</span> (event.getAction() == KeyEvent.ACTION_DOWN &amp;&amp; event.getRepeatCount() == <span class="number">0</span>) &#123;</span><br><span class="line">                    adjustSuggestedStreamVolume(AudioManager.ADJUST_TOGGLE_MUTE,</span><br><span class="line">                            AudioManager.USE_DEFAULT_STREAM_TYPE, flags, callingPackage, caller,</span><br><span class="line">                            Binder.getCallingUid(), <span class="literal">true</span>, VOL_ADJUST_NORMAL);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                Log.e(TAG, <span class="string">&quot;Invalid key code &quot;</span> + event.getKeyCode() + <span class="string">&quot; sent by &quot;</span> + callingPackage);</span><br><span class="line">                <span class="keyword">return</span>; <span class="comment">// not needed but added if code gets added below this switch statement</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>在handleVolumeKey()方法里面，调用了adjustSuggestedStreamVolume方法，参数含义如下：</p>
<table>
<thead>
<tr>
<th>按键类型</th>
<th>Audio Service操作类型</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>KEYCODE_VOLUME_UP</td>
<td>AudioManager.ADJUST_RAISE</td>
<td>音量减</td>
</tr>
<tr>
<td>KEYCODE_VOLUME_DOWN</td>
<td>AudioManager.ADJUST_LOWER</td>
<td>音量加</td>
</tr>
<tr>
<td>KEYCODE_VOLUME_MUTE</td>
<td>AudioManager.ADJUST_TOGGLE_MUTE</td>
<td>改变静音状态</td>
</tr>
</tbody></table>
<p>在按键的处理过程中，并没有将相应的code传递给AudioService，而是使用了相关的定义，将KEYCODE_VOLUME_UP等操作转化为了ADJUST_RAISE等。而flag存储了一些对音量的要求或者信息。</p>
<p><strong>frameworks/base/media/java/android/media/AudioManager.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Increase the ringer volume.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">ADJUST_RAISE</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Decrease the ringer volume.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">ADJUST_LOWER</span> <span class="operator">=</span> -<span class="number">1</span>;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Maintain the previous ringer volume. This may be useful when needing to</span></span><br><span class="line"><span class="comment"> * show the volume toast without actually modifying the volume.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">ADJUST_SAME</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Mute the volume. Has no effect if the stream is already muted.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">ADJUST_MUTE</span> <span class="operator">=</span> -<span class="number">100</span>;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Unmute the volume. Has no effect if the stream is not muted.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">ADJUST_UNMUTE</span> <span class="operator">=</span> <span class="number">100</span>;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Toggle the mute state. If muted the stream will be unmuted. If not muted</span></span><br><span class="line"><span class="comment"> * the stream will be muted.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">ADJUST_TOGGLE_MUTE</span> <span class="operator">=</span> <span class="number">101</span>;</span><br></pre></td></tr></table></figure>

<h4 id="2-adjustSuggestedStreamVolume"><a href="#2-adjustSuggestedStreamVolume" class="headerlink" title="2 adjustSuggestedStreamVolume"></a>2 adjustSuggestedStreamVolume</h4><p>接下来就到了AudioService的adjustSuggestedStreamVolume方法里面了:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">adjustSuggestedStreamVolume</span><span class="params">(<span class="type">int</span> direction, <span class="type">int</span> suggestedStreamType, <span class="type">int</span> flags,</span></span><br><span class="line"><span class="params">            String callingPackage, String caller, <span class="type">int</span> uid)</span> &#123;</span><br><span class="line">   </span><br><span class="line">        <span class="comment">//   ...省略部分代码</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">int</span> streamType;</span><br><span class="line">        <span class="keyword">if</span> (mUserSelectedVolumeControlStream) &#123; <span class="comment">// implies mVolumeControlStream != -1</span></span><br><span class="line">            streamType = mVolumeControlStream;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 这里获取到，可能是活动状态的音频流，但是不确定，还有待进一步确认</span></span><br><span class="line">            <span class="keyword">final</span> <span class="type">int</span> <span class="variable">maybeActiveStreamType</span> <span class="operator">=</span> getActiveStreamType(suggestedStreamType);  </span><br><span class="line">            <span class="keyword">final</span> <span class="type">boolean</span> activeForReal;</span><br><span class="line">            <span class="keyword">if</span> (maybeActiveStreamType == AudioSystem.STREAM_MUSIC) &#123;</span><br><span class="line">                <span class="comment">// 这里如果STREAM_MUSIC现在在AudioFlinger处理的流中或在最后的0ms中处于活动状态，则为true。</span></span><br><span class="line">                activeForReal = isAfMusicActiveRecently(<span class="number">0</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// 会调用native方法，不深究</span></span><br><span class="line">                activeForReal = AudioSystem.isStreamActive(maybeActiveStreamType, <span class="number">0</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 如果activeForReal为true或者mVolumeControlStream为-1</span></span><br><span class="line">            <span class="comment">// 那么确定要操作修改的流类型为maybeActiveStreamType对应的流类型</span></span><br><span class="line">            <span class="keyword">if</span> (activeForReal || mVolumeControlStream == -<span class="number">1</span>) &#123;</span><br><span class="line">                streamType = maybeActiveStreamType;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// activeForReal为false并且mVolumeControlStream不为-1</span></span><br><span class="line">                <span class="comment">// 表示用户点击了音量进度条，这时候要操作修改的流类型为mVolumeControlStream对应的流类型</span></span><br><span class="line">                streamType = mVolumeControlStream;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">isMute</span> <span class="operator">=</span> isMuteAdjust(direction);</span><br><span class="line">        <span class="comment">// 确保我们获取到的流类型是有效的</span></span><br><span class="line">        ensureValidStreamType(streamType);</span><br><span class="line">        <span class="comment">// 将我们获取到的流，进行流映射，拿到最终需要操作的流类型</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">int</span> <span class="variable">resolvedStream</span> <span class="operator">=</span> mStreamVolumeAlias[streamType];</span><br><span class="line">        <span class="comment">// Play sounds on STREAM_RING only.</span></span><br><span class="line">        <span class="keyword">if</span> ((flags &amp; AudioManager.FLAG_PLAY_SOUND) != <span class="number">0</span> &amp;&amp;</span><br><span class="line">                resolvedStream != AudioSystem.STREAM_RING) &#123;</span><br><span class="line">            flags &amp;= ~AudioManager.FLAG_PLAY_SOUND;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// For notifications/ring, show the ui before making any adjustments</span></span><br><span class="line">        <span class="comment">// Don&#x27;t suppress mute/unmute requests</span></span><br><span class="line">        <span class="comment">// 通知和响铃，调整音量之前先显示UI。</span></span><br><span class="line">        <span class="keyword">if</span> (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) &#123;</span><br><span class="line">            <span class="comment">// 将操作置为ADJUST_SAME（ADJUST_SAME = 0）；</span></span><br><span class="line">            direction = <span class="number">0</span>;</span><br><span class="line">            flags &amp;= ~AudioManager.FLAG_PLAY_SOUND;</span><br><span class="line">            flags &amp;= ~AudioManager.FLAG_VIBRATE;</span><br><span class="line">            <span class="keyword">if</span> (DEBUG_VOL) Log.d(TAG, <span class="string">&quot;Volume controller suppressed adjustment&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 这里设置音量</span></span><br><span class="line">        adjustStreamVolume(streamType, direction, flags, callingPackage, caller, uid);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>这里需要注意的有以下几点：</p>
<ol>
<li>mUserSelectedVolumeControlStream：这个属性表示，用户是否已通过单击音量进度条选择音量流来更改由音量键控制的音量，如果mVolumeControlStream为-1，那么mUserSelectedVolumeControlStream 为false。说简单点，当用户点击了某个音量条，这时再去按下音量加减，这个时候调节的是你点击的那个流类型。</li>
<li>getActiveStreamType：获取我们要控制的流的类型，当然，只是可能需要控制的流类型，还需要进一步确认。</li>
<li>mStreamVolumeAlias[streamType]：进行流映射，获取最终需要调整的流类型</li>
<li>suppressAdjustment：字面意思为抑制调整，为什么抑制调整呢，说白了，当我们没有显示音量的UI进度条的时候，不管我们是加音量还是减音量（注意：静音和解静音除外），这个时候都是先显示音量条，而不去改变音量的大小。所以当这个方法返回true的时候， direction = 0，这里direction为0就表示我们的操作为ADJUST_SAME，大家可以在AudioManager里面查看ADJUST_SAME的注释就知道这个操作表示只弹出UI但是不调整音量大小。</li>
<li>adjustStreamVolume：进行音量的调整</li>
</ol>
<h4 id="3-adjustStreamVolume"><a href="#3-adjustStreamVolume" class="headerlink" title="3 adjustStreamVolume"></a>3 adjustStreamVolume</h4><p>进入到了adjustStreamVolume函数，该函数比较长，得分片段分析。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">adjustStreamVolume</span><span class="params">(<span class="type">int</span> streamType, <span class="type">int</span> direction, <span class="type">int</span> flags,</span></span><br><span class="line"><span class="params">            String callingPackage, String caller, <span class="type">int</span> uid)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (mUseFixedVolume) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        ensureValidDirection(direction);</span><br><span class="line">        ensureValidStreamType(streamType);</span><br><span class="line">        <span class="type">boolean</span> <span class="variable">isMuteAdjust</span> <span class="operator">=</span> isMuteAdjust(direction);</span><br><span class="line">        <span class="keyword">if</span> (isMuteAdjust &amp;&amp; !isStreamAffectedByMute(streamType)) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// use stream type alias here so that streams with same alias have the same behavior,</span></span><br><span class="line">        <span class="comment">// including with regard to silent mode control (e.g the use of STREAM_RING below and in</span></span><br><span class="line">        <span class="comment">// checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION)</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">streamTypeAlias</span> <span class="operator">=</span> mStreamVolumeAlias[streamType];</span><br><span class="line">        <span class="type">VolumeStreamState</span> <span class="variable">streamState</span> <span class="operator">=</span> mStreamStates[streamTypeAlias];</span><br><span class="line">        ......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>mUseFixedVolume：表示使用固定音量，我们无法修改音量</li>
<li>mStreamVolumeAlias[streamType]：进行音频流的映射，拿到映射后的音频流</li>
<li>mStreamStates[streamTypeAlias]：mStreamStates是一个存储VolumeStreamState类型的数组，保存着每个音频流的状态。VolumeStreamState是AudioService的一个内部类，里面保存单个音频流的所有信息，比如流类型，音量大小，mute状态等。并且相同的流类型，在不同的设备，大小也是不一样的（比如耳机和扬声器，媒体音量大小是不一样的），这也是在VolumeStreamState里面去维护的。）</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">adjustStreamVolume</span><span class="params">(<span class="type">int</span> streamType, <span class="type">int</span> direction, <span class="type">int</span> flags,</span></span><br><span class="line"><span class="params">        String callingPackage, String caller, <span class="type">int</span> uid)</span> &#123;</span><br><span class="line">        ......</span><br><span class="line">        <span class="keyword">final</span> <span class="type">int</span> <span class="variable">device</span> <span class="operator">=</span> getDeviceForStream(streamTypeAlias);</span><br><span class="line">        <span class="type">int</span> <span class="variable">aliasIndex</span> <span class="operator">=</span> streamState.getIndex(device);</span><br><span class="line">        <span class="type">boolean</span> <span class="variable">adjustVolume</span> <span class="operator">=</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="type">int</span> step;</span><br><span class="line">        <span class="comment">// skip a2dp absolute volume control request when the device</span></span><br><span class="line">        <span class="comment">// is not an a2dp device</span></span><br><span class="line">        <span class="comment">// 如果不是蓝牙设备，则跳过音量绝对控制请求 </span></span><br><span class="line">        <span class="keyword">if</span> ((device &amp; AudioSystem.DEVICE_OUT_ALL_A2DP) == <span class="number">0</span> &amp;&amp;</span><br><span class="line">            (flags &amp; AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// If we are being called by the system (e.g. hardware keys) check for current user</span></span><br><span class="line">        <span class="comment">// so we handle user restrictions correctly.</span></span><br><span class="line">        <span class="comment">// uid判断，方便去做用户权限处理</span></span><br><span class="line">        <span class="keyword">if</span> (uid == android.os.Process.SYSTEM_UID) &#123;</span><br><span class="line">            uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 权限处理</span></span><br><span class="line">        <span class="keyword">if</span> (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage)</span><br><span class="line">                != AppOpsManager.MODE_ALLOWED) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        .......</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>getDeviceForStream：通过流类型去获取设备类型这里去获取设备类型，通过流类型获取到了对应的VolumeStreamState的实例，然后调用了其observeDevicesForStream_syncVSS方法去获取devices，在observeDevicesForStream_syncVSS方法里面，又会去调用AudioSystem的getDevicesForStream去获取设备，这是个native方法。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">adjustStreamVolume</span><span class="params">(<span class="type">int</span> streamType, <span class="type">int</span> direction, <span class="type">int</span> flags,</span></span><br><span class="line"><span class="params">        String callingPackage, String caller, <span class="type">int</span> uid)</span> &#123;</span><br><span class="line">        ......</span><br><span class="line">        <span class="comment">// 清除掉任何待处理的音量命令</span></span><br><span class="line">        <span class="keyword">synchronized</span> (mSafeMediaVolumeState) &#123;</span><br><span class="line">            mPendingVolumeCommand = <span class="literal">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 表示不是固定音量 </span></span><br><span class="line">        flags &amp;= ~AudioManager.FLAG_FIXED_VOLUME;</span><br><span class="line">        <span class="comment">// 如果是多媒体音量，并且是使用固定音量的设备</span></span><br><span class="line">        <span class="keyword">if</span> ((streamTypeAlias == AudioSystem.STREAM_MUSIC) &amp;&amp;</span><br><span class="line">               ((device &amp; mFixedVolumeDevices) != <span class="number">0</span>)) &#123;</span><br><span class="line">            <span class="comment">// 加上表示固定音量的flag</span></span><br><span class="line">            flags |= AudioManager.FLAG_FIXED_VOLUME;</span><br><span class="line">            <span class="comment">// Always toggle between max safe volume and 0 for fixed volume devices where safe</span></span><br><span class="line">            <span class="comment">// volume is enforced, and max and 0 for the others.</span></span><br><span class="line">            <span class="comment">// This is simulated by stepping by the full allowed volume range</span></span><br><span class="line">            <span class="keyword">if</span> (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE &amp;&amp;</span><br><span class="line">                    (device &amp; mSafeMediaVolumeDevices) != <span class="number">0</span>) &#123;</span><br><span class="line">                step = safeMediaVolumeIndex(device);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                step = streamState.getMaxIndex();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (aliasIndex != <span class="number">0</span>) &#123;</span><br><span class="line">                aliasIndex = step;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// convert one UI step (+/-1) into a number of internal units on the stream alias</span></span><br><span class="line">            <span class="comment">// 如果不是多媒体音量，或者是多媒体音量但是不是固定音量的设备时</span></span><br><span class="line">            <span class="comment">// 将音量值的步进量从源流类型变换到目标流类型下，由于不同的流类型的音量调节范围不同，所以这个转换是必需的</span></span><br><span class="line">            step = rescaleIndex(<span class="number">10</span>, streamType, streamTypeAlias);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 情景模式的处理</span></span><br><span class="line">        <span class="comment">// If either the client forces allowing ringer modes for this adjustment,</span></span><br><span class="line">        <span class="comment">// or the stream type is one that is affected by ringer modes</span></span><br><span class="line">        <span class="keyword">if</span> (((flags &amp; AudioManager.FLAG_ALLOW_RINGER_MODES) != <span class="number">0</span>) ||</span><br><span class="line">                (streamTypeAlias == getUiSoundsStreamType())) &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">ringerMode</span> <span class="operator">=</span> getRingerModeInternal();</span><br><span class="line">            <span class="comment">// do not vibrate if already in vibrate mode</span></span><br><span class="line">            <span class="comment">// 如果已经是震动模式，则不进行震动</span></span><br><span class="line">            <span class="keyword">if</span> (ringerMode == AudioManager.RINGER_MODE_VIBRATE) &#123;</span><br><span class="line">                flags &amp;= ~AudioManager.FLAG_VIBRATE;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// Check if the ringer mode handles this adjustment. If it does we don&#x27;t</span></span><br><span class="line">            <span class="comment">// need to adjust the volume further.</span></span><br><span class="line">            <span class="comment">// 根据我们的操作来检查是否需要切换情景模式</span></span><br><span class="line">            <span class="keyword">final</span> <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> checkForRingerModeChange(aliasIndex, direction, step,</span><br><span class="line">                    streamState.mIsMuted, callingPackage, flags);</span><br><span class="line">            adjustVolume = (result &amp; FLAG_ADJUST_VOLUME) != <span class="number">0</span>;</span><br><span class="line">            <span class="comment">// If suppressing a volume adjustment in silent mode, display the UI hint</span></span><br><span class="line">            <span class="keyword">if</span> ((result &amp; AudioManager.FLAG_SHOW_SILENT_HINT) != <span class="number">0</span>) &#123;</span><br><span class="line">                flags |= AudioManager.FLAG_SHOW_SILENT_HINT;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// If suppressing a volume down adjustment in vibrate mode, display the UI hint</span></span><br><span class="line">            <span class="keyword">if</span> ((result &amp; AudioManager.FLAG_SHOW_VIBRATE_HINT) != <span class="number">0</span>) &#123;</span><br><span class="line">                flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// If the ringermode is suppressing media, prevent changes</span></span><br><span class="line">        <span class="comment">// 勿扰模式</span></span><br><span class="line">        <span class="keyword">if</span> (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) &#123;</span><br><span class="line">            adjustVolume = <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 获取旧的音量大小</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">oldIndex</span> <span class="operator">=</span> mStreamStates[streamType].getIndex(device);</span><br><span class="line">        ......</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>固定音量设备的处理</li>
<li>音量步进的处理：rescaleIndex方法，将音量值的步进量从源流类型变换到目标流类型下，由于不同的流类型的音量调节范围不同，所以这个转换是必需的。计算按下音量键的音量步进值。这个步进值是10而不是1。在VolumeStreamState中保存的音量值是其实际值的10倍，这是为了在不同流类型之间进行音量转化时能够保证一定精度的一种实现。可以理解为在转化过程中保留了小数点后一位的精度。</li>
<li>情景模式处理：情景模式的处理就涉及到了音量的调整，以及情景模式的切换，在切换情景模式（震动到响铃除外）的时候，是没有去调整音量的。通过adjustVolume = (result &amp; FLAG_ADJUST_VOLUME) != 0这一句体现出来的。</li>
<li>勿扰模式处理：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">adjustStreamVolume</span><span class="params">(<span class="type">int</span> streamType, <span class="type">int</span> direction, <span class="type">int</span> flags,</span></span><br><span class="line"><span class="params">        String callingPackage, String caller, <span class="type">int</span> uid)</span> &#123;</span><br><span class="line">        ......</span><br><span class="line">   </span><br><span class="line">        <span class="keyword">if</span> (adjustVolume</span><br><span class="line">                &amp;&amp; (direction != AudioManager.ADJUST_SAME) &amp;&amp; (keyEventMode != VOL_ADJUST_END)) &#123;</span><br><span class="line">            mAudioHandler.removeMessages(MSG_UNMUTE_STREAM);</span><br><span class="line">            <span class="comment">// 处理静音调整</span></span><br><span class="line">            <span class="keyword">if</span> (isMuteAdjust) &#123;</span><br><span class="line">                <span class="type">boolean</span> state;</span><br><span class="line">                <span class="keyword">if</span> (direction == AudioManager.ADJUST_TOGGLE_MUTE) &#123;</span><br><span class="line">                    state = !streamState.mIsMuted;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    state = direction == AudioManager.ADJUST_MUTE;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (streamTypeAlias == AudioSystem.STREAM_MUSIC) &#123;</span><br><span class="line">                    setSystemAudioMute(state);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">stream</span> <span class="operator">=</span> <span class="number">0</span>; stream &lt; mStreamStates.length; stream++) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (streamTypeAlias == mStreamVolumeAlias[stream]) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (!(readCameraSoundForced()</span><br><span class="line">                                    &amp;&amp; (mStreamStates[stream].getStreamType()</span><br><span class="line">                                        == AudioSystem.STREAM_SYSTEM_ENFORCED))) &#123;</span><br><span class="line">                            <span class="comment">// 这里获取当前流对应的VolumeStreamState实例，然后去调用mute方法</span></span><br><span class="line">                            <span class="comment">// 最终也会到AudioSystem去调用native方法</span></span><br><span class="line">                            mStreamStates[stream].mute(state);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> ((direction == AudioManager.ADJUST_RAISE) &amp;&amp;</span><br><span class="line">                    !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) &#123;</span><br><span class="line">                <span class="comment">// 安全音量提示，音量增加的时候才会去检测</span></span><br><span class="line">                Log.e(TAG, <span class="string">&quot;adjustStreamVolume() safe volume index = &quot;</span> + oldIndex);</span><br><span class="line">                mVolumeController.postDisplaySafeVolumeWarning(flags);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (!isFullVolumeDevice(device)</span><br><span class="line">                    &amp;&amp; (streamState.adjustIndex(direction * step, device, caller,</span><br><span class="line">                            hasModifyAudioSettings)</span><br><span class="line">                            || streamState.mIsMuted)) &#123;</span><br><span class="line">                <span class="comment">// Post message to set system volume (it in turn will post a</span></span><br><span class="line">                <span class="comment">// message to persist).</span></span><br><span class="line">                <span class="keyword">if</span> (streamState.mIsMuted) &#123;</span><br><span class="line">                    <span class="comment">// Unmute the stream if it was previously muted</span></span><br><span class="line">                    <span class="keyword">if</span> (direction == AudioManager.ADJUST_RAISE) &#123;</span><br><span class="line">                        <span class="comment">// unmute immediately for volume up</span></span><br><span class="line">                        streamState.mute(<span class="literal">false</span>);</span><br><span class="line">                    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (direction == AudioManager.ADJUST_LOWER) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (mIsSingleVolume) &#123;</span><br><span class="line">                            sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE,</span><br><span class="line">                                    streamTypeAlias, flags, <span class="literal">null</span>, UNMUTE_STREAM_DELAY);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 设置音量到底层</span></span><br><span class="line">                sendMsg(mAudioHandler,</span><br><span class="line">                        MSG_SET_DEVICE_VOLUME,</span><br><span class="line">                        SENDMSG_QUEUE,</span><br><span class="line">                        device,</span><br><span class="line">                        <span class="number">0</span>,</span><br><span class="line">                        streamState,</span><br><span class="line">                        <span class="number">0</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="type">int</span> <span class="variable">newIndex</span> <span class="operator">=</span> mStreamStates[streamType].getIndex(device);</span><br><span class="line">            <span class="comment">// hdmi音量更新，这个不过多讲解</span></span><br><span class="line">            <span class="comment">// Check if volume update should be send to AVRCP</span></span><br><span class="line">            <span class="keyword">if</span> (streamTypeAlias == AudioSystem.STREAM_MUSIC</span><br><span class="line">                    &amp;&amp; AudioSystem.DEVICE_OUT_ALL_A2DP_SET.contains(device)</span><br><span class="line">                    &amp;&amp; (flags &amp; AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (DEBUG_VOL) &#123;</span><br><span class="line">                    Log.d(TAG, <span class="string">&quot;adjustSreamVolume: postSetAvrcpAbsoluteVolumeIndex index=&quot;</span></span><br><span class="line">                            + newIndex + <span class="string">&quot;stream=&quot;</span> + streamType);</span><br><span class="line">                &#125;</span><br><span class="line">                mDeviceBroker.postSetAvrcpAbsoluteVolumeIndex(newIndex / <span class="number">10</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// Check if volume update should be send to Hearing Aid</span></span><br><span class="line">            <span class="keyword">if</span> (device == AudioSystem.DEVICE_OUT_HEARING_AID) &#123;</span><br><span class="line">                <span class="comment">// only modify the hearing aid attenuation when the stream to modify matches</span></span><br><span class="line">                <span class="comment">// the one expected by the hearing aid</span></span><br><span class="line">                <span class="keyword">if</span> (streamType == getHearingAidStreamType()) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (DEBUG_VOL) &#123;</span><br><span class="line">                        Log.d(TAG, <span class="string">&quot;adjustSreamVolume postSetHearingAidVolumeIndex index=&quot;</span></span><br><span class="line">                                + newIndex + <span class="string">&quot; stream=&quot;</span> + streamType);</span><br><span class="line">                    &#125;</span><br><span class="line">                    mDeviceBroker.postSetHearingAidVolumeIndex(newIndex, streamType);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// Check if volume update should be sent to Hdmi system audio.</span></span><br><span class="line">            <span class="keyword">if</span> (streamTypeAlias == AudioSystem.STREAM_MUSIC) &#123;</span><br><span class="line">                setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">int</span> <span class="variable">newIndex</span> <span class="operator">=</span> mStreamStates[streamType].getIndex(device);</span><br><span class="line">           <span class="keyword">if</span> (adjustVolume) &#123;</span><br><span class="line">            <span class="keyword">synchronized</span> (mHdmiClientLock) &#123;</span><br><span class="line">                <span class="keyword">if</span> (mHdmiManager != <span class="literal">null</span>) &#123;</span><br><span class="line">                    <span class="comment">// mHdmiCecSink true =&gt; mHdmiPlaybackClient != null</span></span><br><span class="line">                    <span class="keyword">if</span> (mHdmiCecSink</span><br><span class="line">                            &amp;&amp; mHdmiCecVolumeControlEnabled</span><br><span class="line">                            &amp;&amp; streamTypeAlias == AudioSystem.STREAM_MUSIC</span><br><span class="line">                            <span class="comment">// vol change on a full volume device</span></span><br><span class="line">                            &amp;&amp; isFullVolumeDevice(device)) &#123;</span><br><span class="line">                        <span class="type">int</span> <span class="variable">keyCode</span> <span class="operator">=</span> KeyEvent.KEYCODE_UNKNOWN;</span><br><span class="line">                        <span class="keyword">switch</span> (direction) &#123;</span><br><span class="line">                            <span class="keyword">case</span> AudioManager.ADJUST_RAISE:</span><br><span class="line">                                keyCode = KeyEvent.KEYCODE_VOLUME_UP;</span><br><span class="line">                                <span class="keyword">break</span>;</span><br><span class="line">                            <span class="keyword">case</span> AudioManager.ADJUST_LOWER:</span><br><span class="line">                                keyCode = KeyEvent.KEYCODE_VOLUME_DOWN;</span><br><span class="line">                                <span class="keyword">break</span>;</span><br><span class="line">                            <span class="keyword">case</span> AudioManager.ADJUST_TOGGLE_MUTE:</span><br><span class="line">                                keyCode = KeyEvent.KEYCODE_VOLUME_MUTE;</span><br><span class="line">                                <span class="keyword">break</span>;</span><br><span class="line">                            <span class="keyword">default</span>:</span><br><span class="line">                                <span class="keyword">break</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                        <span class="keyword">if</span> (keyCode != KeyEvent.KEYCODE_UNKNOWN) &#123;</span><br><span class="line">                            <span class="keyword">final</span> <span class="type">long</span> <span class="variable">ident</span> <span class="operator">=</span> Binder.clearCallingIdentity();</span><br><span class="line">                            <span class="keyword">try</span> &#123;</span><br><span class="line">                                <span class="keyword">final</span> <span class="type">long</span> <span class="variable">time</span> <span class="operator">=</span> java.lang.System.currentTimeMillis();</span><br><span class="line">                                <span class="keyword">switch</span> (keyEventMode) &#123;</span><br><span class="line">                                    <span class="keyword">case</span> VOL_ADJUST_NORMAL:</span><br><span class="line">                                        mHdmiPlaybackClient.sendVolumeKeyEvent(keyCode, <span class="literal">true</span>);</span><br><span class="line">                                        mHdmiPlaybackClient.sendVolumeKeyEvent(keyCode, <span class="literal">false</span>);</span><br><span class="line">                                        <span class="keyword">break</span>;</span><br><span class="line">                                    <span class="keyword">case</span> VOL_ADJUST_START:</span><br><span class="line">                                        mHdmiPlaybackClient.sendVolumeKeyEvent(keyCode, <span class="literal">true</span>);</span><br><span class="line">                                        <span class="keyword">break</span>;</span><br><span class="line">                                    <span class="keyword">case</span> VOL_ADJUST_END:</span><br><span class="line">                                        mHdmiPlaybackClient.sendVolumeKeyEvent(keyCode, <span class="literal">false</span>);</span><br><span class="line">                                        <span class="keyword">break</span>;</span><br><span class="line">                                    <span class="keyword">default</span>:</span><br><span class="line">                                        Log.e(TAG, <span class="string">&quot;Invalid keyEventMode &quot;</span> + keyEventMode);</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                                Binder.restoreCallingIdentity(ident);</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">if</span> (streamTypeAlias == AudioSystem.STREAM_MUSIC</span><br><span class="line">                            &amp;&amp; (oldIndex != newIndex || isMuteAdjust)) &#123;</span><br><span class="line">                        maybeSendSystemAudioStatusCommand(isMuteAdjust);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 通知外界音量发生变化 </span></span><br><span class="line">        sendVolumeUpdate(streamType, oldIndex, newIndex, flags, device);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2024/01/06/2024-01-06/">http://longzhiye.top/2024/01/06/2024-01-06/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a><a class="mdui-ripple article_tags-link" href="/tags/Launcher/" rel="tag">Launcher</a><a class="mdui-ripple article_tags-link" href="/tags/Settings/" rel="tag">Settings</a><a class="mdui-ripple article_tags-link" href="/tags/framework/" rel="tag">framework</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2024/01/07/2024-01-07/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 10 音量UI更新解析'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2024/01/01/2024-01-01/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 10.0 锁屏壁纸 LockscreenWallpaper'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
